<?php
if($_REQUEST['_a'] == "del") {
	$sql = "delete from page where id='".$_REQUEST['_d']."'";
	mysql_query($sql) or die(mysql_error());
	$sql = "delete from section where page_name='".$_REQUEST['_d']."'";
	mysql_query($sql) or die(mysql_error());
	
	$sql = "SELECT * FROM banner_ads WHERE id='".$_REQUEST['_d']."'";
	$res = mysql_query($sql) or die(mysql_error());
	$data = mysql_fetch_array($res);
	
	if($data['upload_file']!="") {
		if(file_exists("../uploads/banner/".$data['upload_file'])) {
			unlink("../uploads/banner/".$data['upload_file']);
		}
	}
	
	$sql = "delete from map_ads where page_name='".$_REQUEST['_d']."'";
	mysql_query($sql) or die(mysql_error());
}

if($_REQUEST['_a'] == "ord") {
	changePageOrder($_REQUEST['_m'], $_REQUEST['_d'], $_REQUEST['_o_id'],$_REQUEST['_show'],$_REQUEST['_id']);
}
function changePageOrder($order_by, $id, $order_id,$is_sub="",$sub_id="")
{
	if($is_sub == "sub_page") {
		$where_cond = " and parent_page = '".$sub_id."'";
	}
	else {
		$where_cond = " and parent_page = '0'";
	}
	
	if($order_by == 'down') {
		$sql = "SELECT id,order_id FROM page WHERE order_id > ".$order_id." $where_cond ORDER BY order_id ASC LIMIT 0,1";
		$res = mysql_query($sql);
		$data = mysql_fetch_array($res);
		$order_id_new = $data['order_id'];
		$new_id = $data['id'];
		$order_id_old = $order_id;
		
		$upd_array = "update page set order_id = '".$order_id_old."' WHERE id = ".$new_id;
		$ret = mysql_query($upd_array);
		$upd_array = "update page set order_id = '".$order_id_new."' WHERE id = ".$id;
		$ret = mysql_query($upd_array);
	}
	else {
		$sql = "SELECT id,order_id FROM page WHERE order_id < ".$order_id." $where_cond ORDER BY order_id DESC LIMIT 0,1";
		$res = mysql_query($sql);
		$data = mysql_fetch_array($res);
		$order_id_new = $data['order_id'];
		$new_id = $data['id'];
		$order_id_old = $order_id;
		$upd_array = "update page set order_id = '".$order_id_old."' WHERE id = ".$new_id;
		$ret = mysql_query($upd_array);
		$upd_array = "update page set order_id = '".$order_id_new."' WHERE id = ".$id;
		$ret = mysql_query($upd_array);
	}
}
?>
<script type="text/javascript">
function submitPage()
{
	var page_id = document.getElementById('page_name').value;
	if(page_id != 0) {
		location.href = "index.php?_show=sub_page&_id="+page_id;
	}
	else {
		location.href = "index.php";	
	}
}
</script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<form name="nnf" method="post" action="#">
<table border="0" width="100%" align="center">
<tr>
	<td width="46%" align="left"><span style="color:#1B78B7; font-size:14px; font-weight:bold">
    Manage Pages</span>&nbsp; 
	<!--<input type="button" name="addnew" value="Add New" onclick="return show();" />-->
	</td>
    <td align="right" class="font12">
    <select name="page_name" id="page_name" onchange="submitPage()">
    <option value="0">select</option>
    <?php 
	$sql = "select a.id,a.page_name from page a inner join page b on (a.id = b.parent_page) where a.active_status = '1' and b.active_status = '1' group by a.id";
	$qry = mysql_query($sql) or die(mysql_error());
	while($row=mysql_fetch_array($qry)) {
		if($_REQUEST['_show'] == "sub_page" and $row['id'] == $_REQUEST['_id']) {
			echo '<option value="'.$row['id'].'" selected>'.$row['page_name'].'</option>';
		}
		else {
			echo '<option value="'.$row['id'].'">'.$row['page_name'].'</option>';
		}
		
	}
	?>
    </select>
    </td>
	<td align="right" class="font12">
    <a href="index.php?tc=add_page" class="font12">Add Pages</a>  |  
    <a href="index.php?tc=section" class="font12">Manage Sections</a></td>
</tr>
<tr>
	<td background="images/topline.gif" colspan="3" height="11"></td>
</tr>    
</table>
<table width="100%" align="center" border="0" class="list">
<thead>
<tr>
	<td align="center" width="30">#</td>
	<td align="center" width="200">Page Name</td>
<!--    <td align="center" width="200">Meta Title</td>
    <td align="center" width="200">Meta Desc</td>-->
    <td align="center" width="150">Order Page</td>
    <td align="center" width="100">Status</td>
    <td align="center" width="100">Action</td>
</tr>
</thead>
<?php
include('includes/function.php');
$self="index.php?tc=index&_st=".$_REQUEST['_st'];
$rowsPerPage = 50;
$pageNum = 1;
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}
if($_GET['page']=="")
{
	$pageNum = 1;
}

/*<!--counting the offset-->*/
$offset = ($pageNum - 1) * $rowsPerPage;

	if($_REQUEST['_show'] == "sub_page") {
		$qry_str = "&_show=sub_page&_id=".$_REQUEST['_id']."";
		$where_cond = " and parent_page = '".$_REQUEST['_id']."'";
	}
	else {
		$where_cond = " and parent_page = '0'";
	}

	$res = mysql_query("select id from page");
	
	$query = mysql_query("select id,`page_name`,`meta_title`, 
						 `meta_content`, `meta_keywords`, `active_status`,order_id 
						 from page where page_name not like 'common' $where_cond 
						 order by order_id asc limit $offset , $rowsPerPage") or die(mysql_error());

	$count = mysql_num_rows($query);
$j=1;
while($q = mysql_fetch_array($query))
{ 
	$rec_num = (($j)+$offset);
	$j++;
	if($rec_num == 1) {
		$order_image = '<a href="index.php?_a=ord&_d='.$q['id'].'&_m=down&_o_id='.$q['order_id'].$qry_str.'" title="Click here to Move Page Down"><img style="border:none;" src="images/move_down.png" alt="Move Down" title="Move Down"/></a>&nbsp;';
	}
	elseif($rec_num == $count) {
		$order_image = '
		<div style="float:left;width:20px">&nbsp;</div>
		<a href="index.php?_a=ord&_d='.$q['id'].'&_m=up&_o_id='.$q['order_id'].$qry_str.'" title="Click here to Move Page Up"><img style="border:none;" src="images/move_up.png" alt="Move Up" title="Move Up"/></a>&nbsp;';
	}
	else {
		$order_image = 
		'<a href="index.php?_a=ord&_d='.$q['id'].'&_m=down&_o_id='.$q['order_id'].$qry_str.'" title="Click here to Move Page Down"><img style="border:none;" src="images/move_down.png" alt="Move Down" title="Move Down"/></a>
		<a href="index.php?_a=ord&_d='.$q['id'].'&_m=up&_o_id='.$q['order_id'].$qry_str.'" title="Click here to Move Page Up"><img style="border:none;" src="images/move_up.png" alt="Move Up" title="Move Up"/></a>';
	}

	if($q["active_status"] == 1) $status = "Activated";
	else $status = "Deactivated";
	?>
    <tr class="font12">
        <td align="center"><?php echo $i+1+$offset;?></td>
        <td align="left" width="250px;"><?php echo $q["page_name"];?></td>
          <!-- <td align="left" width="350px;"><?php echo $q["meta_title"];?></td>
       		<td align="left" width="350px;"><?php echo substr($q["meta_content"],0,15)."...";?></td>-->
             <td align="left"><?php echo $order_image;?></td>
	         <td align="center"><?php echo $status;?></td>
        <td align="center"><a href="index.php?tc=add_page&_d=<?php echo $q['id'].$qry_str;?>" class="font12">Edit</a> | 
        	<a href="index.php?_d=<?php echo $q['id'];?>&_a=del&page=<?php echo $pageNum.$qry_str;?>" onclick="return confirm('Are you sure want to delete?')" class="font12">Delete</a>
        </td>
   	</tr>     
<?php 
	$i++;
}
?>
<?php 
if(mysql_num_rows($res) <= $rowsPerPage){	}
else { ?>
<tr>
    <td colspan="7" align="center"  bgcolor="#E3ECF3">
        <?php echo $paginglink=getPagingLink($res, $rowsPerPage,$pageNum,$self); ?>	
    </td>
</tr>
<?php } ?>
</table>
</form>